#! /usr/bin/env python
import os
import waflib.Utils
from waf_dynamo import apidoc_extract_task, dmsdk_add_files, platform_supports_feature

def configure(conf):
    pass

def remove_from_node_list(lst, name):
    return [x for x in lst if not x.abspath().endswith(name)]

def build(bld):
    script = bld.stlib(features = 'cxx c',
                       includes = '. script',
                       source = bld.path.ant_glob(['*.cpp', 'script/*.proto']),
                       proto_gen_cc = False,
                       proto_gen_py = True,
                       protoc_includes = '. script',
                       uselib = 'LUA',
                       target = 'script')

    if 'web' in bld.env.PLATFORM:
        script.source = remove_from_node_list(script.source, 'script_html5.cpp')
    else:
        script.source = remove_from_node_list(script.source, 'script_html5_js.cpp')

    # luabitop
    if 'lua' in waflib.Utils.to_list(script.env['STLIB_LUA']):
        script.source += bld.path.ant_glob('bitop/*.c')

    # luasocket
    if platform_supports_feature(bld.env.PLATFORM, 'luasocket', {}):
        script.source += bld.path.ant_glob('luasocket/*.c')
        script.source = remove_from_node_list(script.source, os.path.join('luasocket', 'mime.c'))
        script.source = remove_from_node_list(script.source, os.path.join('luasocket', 'serial.c'))
        script.source = remove_from_node_list(script.source, os.path.join('luasocket', 'unix.c'))

        script.cflags = ['-DLUASOCKET_API=']
        if 'win32' in bld.env.PLATFORM:
            script.source = remove_from_node_list(script.source, os.path.join('luasocket', 'usocket.c'))
        else:
            script.source = remove_from_node_list(script.source, os.path.join('luasocket', 'wsocket.c'))


    # lua-cjson
    script.source += bld.path.ant_glob('luacjson/*.c')

    dmsdk_add_files(bld, '${PREFIX}/sdk/include/dmsdk', 'dmsdk')

    # We must manually install script_doc_ddf_pb2.py as intall_path is None above
    # in order to avoid installation of dummylib
    bld.install_files('${PREFIX}/lib/python', 'script/script_doc_ddf_pb2.py')

    bld.add_group()

    apidoc_extract_task(bld,[
        'script.cpp',
        'script_bitop.cpp',
        'script_hash.cpp',
        'script_html5_js.cpp',
        'script_json.cpp',
        'script_msg.cpp',
        'script_sys.cpp',
        'script_timer.cpp',
        'script_vmath.cpp',
        'script_zlib.cpp',
        'script_graphics.cpp',
        'script_types.cpp',
        'luasocket/luasocket.doc_h',
        'script/sys_ddf.proto'])

    bld.install_files('${PREFIX}/include/script', 'script.h')
    bld.install_files('${PREFIX}/include/script', 'http_service.h')

    # for #imports to work for other projects
    # ..first for python
    ddf_ext = bld(
         rule   = 'echo "" > ${TGT}',
         source = 'script/lua_source_ddf.proto script/ddf_script.proto',
         target = '__init__.py',
         shell = True)

    bld.install_files('${PREFIX}/share/proto/script', 'script/ddf_script.proto script/sys_ddf.proto script/lua_source_ddf.proto')
    bld.install_files('${PREFIX}/lib/python/script', '__init__.py')

    if 'web' in bld.env.PLATFORM:
        bld.install_files('${PREFIX}/lib/%s/js' % bld.env.PLATFORM, '../lib/js/library_script.js', postpone = False)

    if not waflib.Options.options.skip_build_tests:
        bld.recurse('test')
